package com.example.chessbattleplatform.web.Sys_User.controller;


import com.example.chessbattleplatform.Pojo.Exception.BusinessException;
import com.example.chessbattleplatform.Pojo.Exception.StatusCode;
import com.example.chessbattleplatform.Pojo.Result.WebResult;
import com.example.chessbattleplatform.utils.ThreadJwtParseUtils;
import com.example.chessbattleplatform.utils.UserLoginHistoryIsCachedUtils;
import com.example.chessbattleplatform.web.Sys_User.Pojo.Vo.UpdateVo;
import com.example.chessbattleplatform.web.Sys_User.Pojo.dto.UpdateEmailRequest;
import com.example.chessbattleplatform.web.Sys_User.Pojo.dto.UpdatePasswordRequest;
import com.example.chessbattleplatform.web.Sys_User.Pojo.dto.UpdateUserNameRequest;
import com.example.chessbattleplatform.web.Sys_User.Service.impl.UserServiceInterface;
import jakarta.validation.Valid;
import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.NotNull;
import jakarta.validation.constraints.Size;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

/**
 * 用户信息修改
 * 需要验证用户身份
 * 路径："/api/user"
 */
@RestController
@RequestMapping("/api/user")
@RequiredArgsConstructor
@Slf4j
public class UserUpdateController {

    private final UserServiceInterface userServiceInterface;
    //缓存
    private final UserLoginHistoryIsCachedUtils userLoginHistoryIsCachedUtils;

    private final ThreadJwtParseUtils threadJwtParseUtils;

    /**
     * 获取用户 token 并验证 在获取用户信息
     */
    @GetMapping("/getUserInfo")

    public WebResult<?> getUserInfo() {
        // 获取线程本地存储
        Long userId = threadJwtParseUtils.getThreadUserId();
        String threadParsePassword = threadJwtParseUtils.getThreadParsePassword();
        return WebResult.success(userServiceInterface.getUserInfo(userId, threadParsePassword));
    }

    /**
     * 修改密码
     *
     * @param updatePasswordRequest {
     *                              oldPassword 旧邮箱
     *                              newPassword 新邮箱
     *                              }
     * @return 统一返回
     */
    @PutMapping("/modifyPassword")
    public WebResult<?> modifyPassword(@Valid @RequestBody UpdatePasswordRequest updatePasswordRequest) {
        log.info("修改密码{}", updatePasswordRequest);
        // 获取线程本地存储
        long userId = threadJwtParseUtils.getThreadUserId();

        UpdateVo updateVo = userServiceInterface.modifyPassword(userId, updatePasswordRequest.getOldPassword(), updatePasswordRequest.getNewPassword());
        // 删除缓存
        userLoginHistoryIsCachedUtils.deleteUserLoginHistory(userId);

        return WebResult.success("密码修改成功", updateVo);
    }

    /**
     * 修改邮箱
     *
     * @param updateEmailRequest {
     *                           oldEmail 旧邮箱
     *                           newEmail 新邮箱
     *                           }
     * @return 统一返回
     */
    @PutMapping("/modifyEmail")
    public WebResult<?> modifyEmail(@Valid @RequestBody UpdateEmailRequest updateEmailRequest) {
        log.info("修改邮箱 {}", updateEmailRequest);
        long userId = threadJwtParseUtils.getThreadUserId();

        UpdateVo updateVo = userServiceInterface.modifyEmail(userId, updateEmailRequest.getOldEmail(), updateEmailRequest.getNewEmail());
        // 删除缓存
        userLoginHistoryIsCachedUtils.deleteUserLoginHistory(userId);

        return WebResult.success("邮箱修改成功", updateVo);
    }

    /**
     * 修改用户名
     *
     * @param updateUserNameRequest {
     *                              oldUserName 旧用户名
     *                              newUserName 新用户名
     *                              }
     * @return 统一返回
     */
    @PutMapping("/modifyUserName")
    public WebResult<?> modifyUserName(@Valid @RequestBody UpdateUserNameRequest updateUserNameRequest) {
        long userId = threadJwtParseUtils.getThreadUserId();

        UpdateVo updateVo = userServiceInterface.modifyUserName(userId, updateUserNameRequest.getOldUserName(), updateUserNameRequest.getNewUserName());
        // 删除缓存
        userLoginHistoryIsCachedUtils.deleteUserLoginHistory(userId);

        return WebResult.success("用户名修改成功", updateVo);
    }

    /**
     * 修改头像
     *
     * @param avatar 头像地址
     * @return 统一返回
     */
    @PutMapping("/modifyUserAvatar")
    public WebResult<?> modifyUserAvatar(@NotBlank(message = "头像不能为空") String avatar) {

        long userId = threadJwtParseUtils.getThreadUserId();


        UpdateVo updateVo = userServiceInterface.modifyUserAvatar(userId, avatar);

        return WebResult.success("用户头像修改成功", updateVo);
    }


    /**
     * 修改简介
     *
     * @param introduction 简介内容
     * @return 统一返回
     */
    @PutMapping("/modifyUserIntroduction")

    public WebResult<?> modifyUserIntroduction(@Size( max = 64, message = "简介需在64字符以内") String introduction) {
        long userId = threadJwtParseUtils.getThreadUserId();

        UpdateVo updateVo = userServiceInterface.modifyUserIntroduction(userId, introduction);

        return WebResult.success("用户简介修改成功", updateVo);
    }

    /**
     * 删除用户
     *
     * @param ConfirmDelete 是否确认删除
     * @return 统一返回
     */
    @DeleteMapping("/deleteUser/")
    public WebResult<?> deleteUser(@NotNull(message = "缺失参数") boolean ConfirmDelete) {
        if (!ConfirmDelete) {
            throw new BusinessException(StatusCode.MISSING_PARAMETER, "参数错误");
        }
        // 获取线程数据
        long userId = threadJwtParseUtils.getThreadUserId();

        // 删除缓存
        userLoginHistoryIsCachedUtils.deleteUserLoginHistory(userId);

        return userServiceInterface.deleteUser(userId);
    }

    /**
     * 退出登录
     *
     * @param ConfirmDelete 是否确认退出
     * @return 统一返回
     */
    @GetMapping("/withdrawalUser")
    public WebResult<?> User(@NotNull(message = "缺失参数") boolean ConfirmDelete) {
        if (!ConfirmDelete) {
            throw new BusinessException(StatusCode.MISSING_PARAMETER, "参数错误");
        }
        // 获取线程数据
        long userId = threadJwtParseUtils.getThreadUserId();

        // 删除缓存
        userLoginHistoryIsCachedUtils.deleteUserLoginHistory(userId);

        return WebResult.success("退出成功");
    }
}
